﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace Empires.Infrastructure.Entities.Repository
{
    [Table("T_Templete")]
    public class T_Templete : IEntity<T_Templete>
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        [Column(TypeName="nvarchar")]
        [StringLength(128)]
        public string Name { get; set; }
        /// <summary>
        /// 样品图片路径
        /// </summary>
        [Column(TypeName = "nvarchar")]
        [StringLength(256)]
        public string Sample { get; set; }
        [Column(TypeName="nvarchar")]
        [StringLength(256)]
        public string Description { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(128)]
        public string Creator { get; set; }
        public DateTime CreateTime { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(256)]
        public string Path { get; set; }
        [Column(TypeName="bit")]
        public bool IsPrivate { get; set; }
        [Column(TypeName = "bit")]
        public bool Available { get; set; }

        public override T_Templete Find(string key)
        {
            int id = Int32.Parse(key);
            return Find(c => c.Id == id);
        }

        public override T_Templete Find(System.Linq.Expressions.Expression<Func<T_Templete, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Templete.FirstOrDefault(where);
                if (item != null)
                {
                    this.Available = item.Available;
                    this.CreateTime = item.CreateTime;
                    this.Creator = item.Creator;
                    this.Description = item.Description;
                    this.Id = item.Id;
                    this.IsPrivate = item.IsPrivate;
                    this.Name = item.Name;
                    this.Sample = item.Sample;
                    this.Path = item.Path;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<T_Templete, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.Templete.Any(where);
            }
        }
    }
}
